<?php
global $_RQ,$_AR;
$_RQ['op']=$_RQ['op']?$_RQ['op']:'status1';
$merchid=$this->checklogin();
if($_RQ['op']=='status1'){
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	$params['merchid']=$merchid;
	if(!empty($_RQ['clearno'])){
		$params['clearno']=trim($_RQ['clearno']);
		$return['params']['clearno']=$_RQ['clearno'];
	}
	if(isset($_RQ['status']) && $_RQ['status']!='all'){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_clearing',$params,array($pindex,$psize),$total,$fields,'',$orderby);	
	$return['status']=array('0'=>'待确认','1'=>'待结算','2'=>'已结算','3'=>'已拒绝','4'=>'已拒绝回退');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'clearno','title'=>'结算编号','type'=>'string'),
			array('name'=>'realprice','title'=>'订单应收','type'=>'string'),
			array('name'=>'ordermoney','title'=>'订单金额','type'=>'string'),
			array('name'=>'inpaymoney','title'=>'线上收款','type'=>'string'),
			array('name'=>'price','title'=>'提现金额','type'=>'string'),
			array('name'=>'realpricerate','title'=>'抽成后金额','type'=>'string'),
			array('name'=>'finalprice','title'=>'最终打款','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		),'action'=>array(
			array('type'=>'href','op'=>'orders','title'=>'订单详情'),
			array('type'=>'modal','op'=>'look','title'=>'查看拒绝理由','show'=>array('status'=>3)),
			array('type'=>'modal','op'=>'addcheck','title'=>'重新申请','show'=>array('status'=>3)),
		)),
		'search'=>array(
			array('name'=>'clearno','width'=>15,'title'=>'结算编号','type'=>'string'),
			array('name'=>'status','width'=>15,'title'=>'状态','type'=>'radio','options'=>'status'),
		)
	);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/apply_list');
	}else{
		$return['htmls']=$htmls;
	}
	$return['pay_types']=$this->pay_types;
	$wxapppay_types=array();
	foreach($return['pay_types'] as $key=>$title){
		$wxapppay_types[]=array('key'=>$key,'title'=>$title);
	}
	$return['wxapppay_types']=$wxapppay_types;
	exi($return);
}
if($_RQ['op']=='orders'){
	$item=pdo_get('jy_merch_clearing',array('merchid'=>$merchid,'id'=>intval($_RQ['id'])));
	$orderids=json_decode($item['orderids'],true);
	if(!$orderids){
		exi('新的结算单，才会有订单数据！');
	}
	$return['list']=pdo_getall('jy_weishop_order',array('uniacid'=>$_SESSION['uniacid'],'id'=>$orderids));
	foreach($return['list'] as &$item){
		$item['member']=cfc('core')->member_show($item['mid']);
		$goods=pdo_get('jy_weishop_order_goods',array('orderid'=>$item['id']),array('goodsid','total'));
		$goodsinfo=pdo_get('jy_weishop_goods',array('id'=>$goods['goodsid']),array('title','image','unit'));
		$item['total']=$goods['total'];
		$item['title']=$goodsinfo['title'];
		$item['image']=$goodsinfo['image'];
		$item['unit']=$goodsinfo['unit'];
		$item['merchtitle']=pdo_getcolumn('jy_merch_user',array('id'=>$item['merchid']),'merchname');
	}
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>$tip,'edit'=>false,'total'=>$total,'psize'=>$psize,'html_to'=>'list_html','list'=>array(
			array('name'=>'ordersn','title'=>'订单号_Order ID','type'=>'string'),
			array('name'=>'member','title'=>'会员信息_Member','type'=>'member'),
			array('name'=>'merchtitle','title'=>'所属商户_Merch','type'=>'string'),
			array('type'=>'img_string','title'=>'商品名称/数量_Goods','img'=>'image',
			'data'=>array(
				array('title'=>'','name'=>'title'),
				array('title'=>'数量：_num：','name'=>'total','unit'=>'unit')
				)
			),
			array('name'=>'price','title'=>'金额_Price','type'=>'string'),
			array('name'=>'status','title'=>'订单状态_status','type'=>'radio','options'=>'status'),
			array('name'=>'createtime','title'=>'下单时间_order time','type'=>'time'),
			array('name'=>'ispay','title'=>'是否支付_Is pay?','type'=>'radio','options'=>'ispay'),
			array('name'=>'paytype','title'=>'支付方式_Pay type','type'=>'radio','options'=>'paytype'),			
		)
		),
	);
	$return['status']=$this->order_status;
	$return['paytype']=array(
		'credit'=>'余额支付',
		'wechat'=>'微信支付',
		'alipay'=>'支付宝支付',
		'yinlian'=>'银联支付',
		'hdfk'=>'货到付款',
	);
	$return['paytype']['peerpay']='找人代付';
	$return['ispay']=array('0'=>'未支付','1'=>'已支付');
	$return['htmls']=$htmls;
	$return['merchs']=$merchs;
	exi($return);
}
if($_RQ['op']=='look'){
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_merch_clearing',array('id'=>$id));
	$return['item']=$item;
	$htmls=array(
		'type'=>'look',
		'desc'=>'退款拒绝理由',
		'look'=>array(
			array('type'=>'text','title'=>'拒绝理由','name'=>'remark'),			
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='addcheck'){
	if($_RQ['postdata']){
		$post=get_postdata();
		$data=array(
			'content'=>$post['content'],
			'status'=>0,
		);
		pdo_update('jy_merch_clearing',$data,array('id'=>$post['id'],'uniacid'=>$_SESSION['uniacid']));
		exi('提交成功！','','status1');
	}
	$id=intval($_RQ['id']);
	$item=pdo_get('jy_merch_clearing',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'申请审核',
		'edit'=>array(
			array('name'=>'clearno','title'=>'结算编号','type'=>'string','read'=>true),
			array('name'=>'realprice','title'=>'订单应收','type'=>'string','read'=>true),
			array('name'=>'price','title'=>'订单实收','type'=>'string','read'=>true),
			array('name'=>'realpricerate','title'=>'抽成后金额','type'=>'string','read'=>true),
			array('name'=>'finalprice','title'=>'最终打款','type'=>'string','read'=>true),
			array('type'=>'text','title'=>'申请事由','name'=>'content'),		
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='status2'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>1);
	$params['merchid']=$merchid;
    $fields=array('id','clearno','merchid','realprice','price','status','realpricerate','finalprice');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_clearing',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	
	$return['status']=array('0'=>'待确认','1'=>'待结算','2'=>'已结算');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			
			
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'list'=>array(
			array('name'=>'clearno','title'=>'结算编号','type'=>'string'),
			array('name'=>'realprice','title'=>'订单应收','type'=>'string'),
			array('name'=>'price','title'=>'订单实收','type'=>'string'),
			array('name'=>'realpricerate','title'=>'抽成后金额','type'=>'string'),
			array('name'=>'finalprice','title'=>'最终打款','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
	);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/apply_list');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='status3'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>2);
	$params['merchid']=$merchid;
    $fields=array('id','clearno','merchid','realprice','price','status','realpricerate','finalprice');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_clearing',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	
	$return['status']=array('0'=>'待确认','1'=>'待结算','2'=>'已结算');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			
			
		),
		'list'=>array('data'=>'list','edit'=>false,'delall'=>false,'del_one'=>false,'list'=>array(
			array('name'=>'clearno','title'=>'结算编号','type'=>'string'),
			array('name'=>'realprice','title'=>'订单应收','type'=>'string'),
			array('name'=>'price','title'=>'订单实收','type'=>'string'),
			array('name'=>'realpricerate','title'=>'抽成后金额','type'=>'string'),
			array('name'=>'finalprice','title'=>'最终打款','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
	);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/apply_list');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='status4'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 10;
	$params=array('uniacid'=>$_SESSION['uniacid'],'status'=>4);
	$params['merchid']=$merchid;
    $fields=array('id','clearno','merchid','realprice','price','status','realpricerate','finalprice');
    $orderby='id desc';
	$total='';
	$return['list']=pdo_getslice('jy_merch_clearing',$params,array($pindex,$psize),$total,$fields,'',$orderby);
	
	$return['status']=array('0'=>'待确认','1'=>'待结算','2'=>'已结算');
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			
			
		),
		'list'=>array('data'=>'list','edit'=>false,'list'=>array(
			array('name'=>'clearno','title'=>'结算编号','type'=>'string'),
			array('name'=>'realprice','title'=>'订单应收','type'=>'string'),
			array('name'=>'price','title'=>'订单实收','type'=>'string'),
			array('name'=>'realpricerate','title'=>'抽成后金额','type'=>'string'),
			array('name'=>'finalprice','title'=>'最终打款','type'=>'string'),
			array('name'=>'status','title'=>'状态','type'=>'radio','options'=>'status'),			
		)),
	);
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	if($_SESSION['os']=='mobile'){
		$return['htmls']=$this->template_url('manage/apply_list');
	}else{
		$return['htmls']=$htmls;
	}
	exi($return);
}
if($_RQ['op']=='post'){
	$post=get_postdata();
	$balance_id=intval($post['id']);
	$remark=$post['remark'];
	$update=array(
			'status'=>0,
			'pay_type'=>$post['pay_type'],
			'realname'=>$post['realname'],
			'alipay'=>$post['alipay'],
			'bankname'=>$post['bankname'],
			'bankcard'=>$post['bankcard'],
			'content'=>$remark,
	);
	$this->update('clearing',$update,array('id'=>$balance_id));
	exi('申请成功！','','status1');
}
if($_RQ['op']=='del'){
	exi('不支持删除','error');
	$id=intval($_RQ['id']);
	if($id){	
		$clearing=pdo_get('jy_merch_clearing',array('id'=>$id));
		if($clearing['status']==0 || $clearing['status']==1){
			pdo_update('jy_weishop_order_goods',array('clearstatus'=>0),array('createtime between'=>array($clearing['starttime'],$clearing['endtime']),'uniacid'=>$_SESSION['uniacid'],'merchid'=>$clearing['merchid']));
		}
		pdo_delete('jy_merch_clearing',array('id'=>$id,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid));
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$clearing=pdo_get('jy_merch_clearing',array('id'=>$id));
				if($clearing['status']==0 || $clearing['status']==1){
					pdo_update('jy_weishop_order_goods',array('clearstatus'=>0),array('createtime between'=>array($clearing['starttime'],$clearing['endtime']),'uniacid'=>$_SESSION['uniacid'],'merchid'=>$clearing['merchid']));
				}
				pdo_delete('jy_merch_clearing',array('id'=>$id,'uniacid'=>$_SESSION['uniacid'],'merchid'=>$merchid));
			}
		}
	}
	exi('删除成功！未结算订单可以重新提交结算！');
}
?>